package com.tongcao.cn.excel;

import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author:tongcao
 * @since:2019/11/25 11:25
 * Description:
 */
@Component
public class PersonExcelProcessor extends AbstractExcelProcessor {


    private PersonRequest rq;
    private PersonResponse rs;


    /**
     * @param request
     * @param response
     * @return
     */
    public boolean deal(PersonRequest request, PersonResponse response) {
        fileSuffix = ".xlsx";
        this.rq = request;
        this.rs = response;
        TaskCallback taskCallback = new TaskCallback();
        //todo
//        process(searchTaskRQ.getTask(), taskCallback);
        return true;
    }


    @Override
    public void process(Task task, TaskCallback taskCallback) {
        super.process(task, taskCallback);
    }

    @Override
    protected byte[] getExcelBytes() {
        int totalCounts = rs.getTotalCount();

        List<PersonVO> voList = Lists.newArrayList();

        if (voList.size() == 0) {
            //解决导出excel表是空List时候，导出excel表无法打开的问题（临时解决方案）
            voList.add(new PersonVO());
        }

        try {
            byte[] resutlBytes = fecthExcelBytes(voList);
            return resutlBytes;
        } catch (IOException e) {
            String err = "获取excel数据信息失败";
        }
        return new byte[0];
    }

    private byte[] fecthExcelBytes(List<PersonVO> list) throws IOException {
        InputStream is = this.getExcelTemplateIs();
        XSSFWorkbook workbook = new XSSFWorkbook(is);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFRow titleRow = sheet.getRow(1);
        if (titleRow != null) {
            titleRow.getCell(0).setCellValue("报表创建于:");
        }
        is.close();
        SXSSFWorkbook wb = new SXSSFWorkbook(workbook);
        wb.setCompressTempFiles(true);

        SXSSFSheet sh = (SXSSFSheet) wb.getSheetAt(0);
        sh.setRandomAccessWindowSize(100);
        for (int i = 0; i < list.size(); i++) {
            PersonVO vo = list.get(i);
            Row row = sh.createRow(i + EXCEL_START_ROW);
            for (int j = 0; j < EXCEL_TOTAL_COL; j++) {
                row.createCell(j);
            }
            try {
                row.getCell(0).setCellValue(list.get(i).getId());
                row.getCell(1).setCellValue(list.get(i).getIdCard());

            } catch (Exception e) {
                continue;
            }
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            wb.write(bos);
        } finally {
            bos.close();
        }
        byte[] bytes = bos.toByteArray();
        return bytes;
    }

    /**
     * @return
     * @Description 从tfs上获取模板
     * @author xiudong.lxd
     */
    public InputStream getExcelTemplateIs() {
        InputStream is = null;
        try {
            is = PersonExcelProcessor.class.getResourceAsStream("/excelTemplate/OrderSearchExportTemplate.xlsx");
        } catch (Exception e) {

        }
        return is;
    }
}